package org.example.sdpback.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.example.sdpback.domain.entity.Iteration;
import org.example.sdpback.domain.entity.Project;
import org.example.sdpback.domain.entity.User;
import org.example.sdpback.domain.vo.IterationVO;
import org.example.sdpback.mapper.IterationMapper;
import org.example.sdpback.service.IterationService;
import org.example.sdpback.service.ProjectService;
import org.example.sdpback.service.UserService;
import org.example.sdpback.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class IterationServiceImpl extends ServiceImpl<IterationMapper, Iteration> implements IterationService {

    private final ProjectService projectService;
    private final UserService userService;

    /**
     * 查询迭代列表
     * @param username
     * @return
     */
    @Override
    public List<IterationVO> getIterationList(String username) {
        List<Iteration> list = null;
        List<IterationVO> iterationVos = null;

        //用户为空，查询所有迭代
        if (username == null || username.equals("")){
            list = this.list();
            iterationVos = BeanUtil.copyToList(list, IterationVO.class);

            // 查询数据为空直接返回
            if (list == null || list.size() == 0){
                return iterationVos;
            }

            //根据迭代查询用户
            List<Long> userIds = list.stream().map(Iteration::getUserId).collect(Collectors.toList()).stream().map(Long::parseLong).collect(Collectors.toList());
            List<User> users = userService.listByIds(userIds);
            Map<Long, User> userMap = users.stream().collect(Collectors.toMap(User::getId, Function.identity()));

            //根据迭代查询项目
            List<Long> projectIds = list.stream().map(Iteration::getProjectId).collect(Collectors.toList()).stream().map(Long::parseLong).collect(Collectors.toList());
            List<Project> projects = projectService.listByIds(projectIds);
            Map<Long, Project> projectMap = projects.stream().collect(Collectors.toMap(Project::getId, Function.identity()));

            //将项目和用户组装成返回对象
            for (IterationVO iterationVO : iterationVos) {
                iterationVO.setCreateUser(userMap.get(Long.valueOf(iterationVO.getUserId())).getUsername());
                iterationVO.setProjectName(projectMap.get(Long.valueOf(iterationVO.getProjectId())).getName());
            }
        }else {
            // 用户不为空，先查用户
            List<User> userList = userService.lambdaQuery().like(User::getUsername, username).list();

            if (userList == null || userList.size() == 0){
                return iterationVos;
            }

            Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));
            List<Long> userIds = userMap.keySet().stream().collect(Collectors.toList());

            // 根据用户查迭代
            List<Iteration> iterationsList = this.lambdaQuery().in(Iteration::getUserId, userIds).list();

            if (iterationsList == null || iterationsList.size() == 0){
                return iterationVos;
            }

            //根据迭代查项目
            iterationVos = BeanUtil.copyToList(iterationsList, IterationVO.class);
            List<Long> projectIds = iterationsList.stream().map(Iteration::getProjectId).collect(Collectors.toList()).stream().map(Long::parseLong).collect(Collectors.toList());
            List<Project> projects = projectService.listByIds(projectIds);
            Map<Long, Project> projectMap = projects.stream().collect(Collectors.toMap(Project::getId, Function.identity()));

            //将用户，项目组装成返回结果
            for (IterationVO iterationVO : iterationVos) {
                iterationVO.setCreateUser(userMap.get(Long.valueOf(iterationVO.getUserId())).getUsername());
                iterationVO.setProjectName(projectMap.get(Long.valueOf(iterationVO.getProjectId())).getName());
            }

        }
        return iterationVos;
    }

    @Override
    public void saveIteraiton(Iteration iteration) {
        User user = ThreadLocalUtil.get();
        Iteration iter = new Iteration();
        iter.setName(iteration.getName());
        iter.setDescription(iteration.getDescription());
        iter.setUserId(user.getId().toString());
        iter.setProjectId(iteration.getProjectId());
        iter.setCreateTime(LocalDateTime.now());
        iter.setUpdateTime(LocalDateTime.now());
        this.save(iter);
    }
}
